package org.example.springboot3.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HtmlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.springboot3.entity.Blog;
import org.example.springboot3.mapper.BlogMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {

    @Resource
    private BlogMapper blogMapper;

    public PageInfo<Blog> selectPage(Blog blog, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> list = blogMapper.selectAll(blog);
        return PageInfo.of(list);
    }

    public List<Blog> selectAll(Blog blog)  {
        List<Blog> blogs = blogMapper.selectAll(blog);
        for (Blog dbblog : blogs) {
            dbblog.setText(HtmlUtil.cleanHtmlTag(dbblog.getContent()));
        }
        return blogs;
    }

    public Blog selectById(Integer id) {
        return blogMapper.selectById(id);
    }

    public void add(Blog blog) {
        blog.setTime(DateUtil.now());
        blogMapper.insert(blog);
    }

    public void deleteById(Integer id) {
        blogMapper.delete(id);
    }

    public void update(Blog blog) {
        blogMapper.update(blog);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    public Integer selectCountByDate(String date) {
        return blogMapper.selectCountByDate(date);
    }
}
